package com.erp.programmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.ProductMapper;
import com.erp.programmer.dao.StockOrderMapper;
import com.erp.programmer.domain.Product;
import com.erp.programmer.domain.StockOrder;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.ProductDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.dto.StockOrderDto;
import com.erp.programmer.service.ProductService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czh
 * @since 2021-05-23
 */
@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private StockOrderMapper stockOrderMapper;



    public ResponseDto<PageDto<ProductDto>> getAll(PageDto<ProductDto> pageDto){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if("brand".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据商品品牌搜索
            queryWrapper.like("brand",pageDto.getSearchContent());
        }
        if(pageDto.getPage() == null){
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Product> list = list(queryWrapper);
        List<ProductDto> productDto = CopyUtil.copyList(list, ProductDto.class);
        productDto.forEach(e->{
            QueryWrapper<StockOrder> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("product_id", e.getId());
            StockOrder stockOrderDb = stockOrderMapper.selectOne(queryWrapper1);
            StockOrderDto stockOrderDto = CopyUtil.copy(stockOrderDb, StockOrderDto.class);
            e.setStockOrderDto(stockOrderDto);
        });
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(productDto);
        return ResponseDto.success(pageDto);
    }

    public ResponseDto<PageDto<ProductDto>> delete(ProductDto productDto){

        Product product = CopyUtil.copy(productDto, Product.class);
        removeById(product);
        PageDto<ProductDto> pageDtoList = getAll(new PageDto<ProductDto>()).getData();
        ResponseDto<PageDto<ProductDto>> responseDto = getAll(pageDtoList);
        responseDto.setMsg("删除成功！");

        return responseDto;
    }

    public ResponseDto<PageDto<ProductDto>> edit(ProductDto productDto){

        Product product = CopyUtil.copy(productDto, Product.class);
        updateById(product);

        //修改库存信息
        QueryWrapper<StockOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", product.getId());
        StockOrder stockOrderDb = stockOrderMapper.selectOne(queryWrapper);
        stockOrderDb.setWarehousePosition(productDto.getStockOrderDto().getWarehousePosition());
        stockOrderMapper.updateById(stockOrderDb);

        PageDto<ProductDto> pageDtoList = getAll(new PageDto<ProductDto>()).getData();
        ResponseDto<PageDto<ProductDto>> responseDto = getAll(pageDtoList);
        responseDto.setMsg("修改成功！");
        return responseDto;
    }

    public ResponseDto<PageDto<ProductDto>> add(ProductDto productDto){

        Product product = CopyUtil.copy(productDto, Product.class);
        String id = String.valueOf(IdWorker.getId());
        product.setId(id);

        StockOrder stockOrder = new StockOrder();
        stockOrder.setNumber("0");
        stockOrder.setProductId(id);
        stockOrder.setWarehousePosition(productDto.getStockOrderDto().getWarehousePosition());
        //插入库存
        stockOrderMapper.insert(stockOrder);

        // 插入商品
        if(!save(product)){
            return ResponseDto.errorByMsg(CodeMsg.PRODUCT_ADD_ERROR);
        }

        PageDto<ProductDto> pageDtoList = getAll(new PageDto<ProductDto>()).getData();
        ResponseDto<PageDto<ProductDto>> responseDto = getAll(pageDtoList);
        responseDto.setMsg("添加成功！");
        return responseDto;
    }

}
